package com.sa.oauth.service.impl;

import cn.dev33.satoken.oauth2.data.loader.SaOAuth2DataLoader;
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
import com.sa.oauth.entity.OAuthClient;
import com.sa.oauth.entity.SysPerson;
import com.sa.oauth.mapper.OAuthClientMapper;
import com.sa.oauth.mapper.SysPersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

@Component
public class SaOAuth2DataLoaderImpl implements SaOAuth2DataLoader {

    @Resource
    private OAuthClientMapper oAuthClientMapper;
    @Autowired
    private Oauth2UserServiceImpl oauth2UserServiceImpl;
    @Autowired
    private SysPersonMapper sysPersonMapper;

    @Override
    public SaClientModel getClientModel(String clientId) {
        OAuthClient clientModel = oAuthClientMapper.getClientModel(clientId);

        SaClientModel saClientModel = new SaClientModel()
                .setClientId(clientModel.getClientId())
                .setClientSecret(clientModel.getClientSecret())
                .setContractScopes(Arrays.asList(clientModel.getContractScopes().split(",")))
                .setAllowGrantTypes(Arrays.asList(clientModel.getGrantTypes().split(",")))
                .setAllowRedirectUris(Arrays.asList(clientModel.getRedirectUris().split(",")));
               // .setAccessTokenTimeout(clientModel.getAccessTokenTimeout())
                //.setRefreshTokenTimeout(clientModel.getRefreshTokenTimeout());


        return saClientModel;
    }

    @Override
    public String getOpenid(String clientId, Object loginId) {
        SysPerson sysPerson = sysPersonMapper.selectById(Long.valueOf(loginId.toString()));
        String openid = sysPerson.getOpenid();
        return openid;
    }
}
